<?php
class TableModel extends CI_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	/*
	 * get number of tables:
	 */
	function table_count(){
		return $this->db->count_all("tables");
	}
	
	/*
	 * get list all tables from db:
	 */
	function fetch_tables($limit, $start){
		$this->db->limit($limit, $start);
		$this->db->select('t._id as _id, t.table_name as name, number, t.manager_id as manager_id, u.first_name as first_name, u.last_name as last_name, t.description as desciption, t.image as image');
		$this->db->from('tables as t');
		$this->db->join('users as u', 'u._id = t.manager_id');
        
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
	}
	
	/*
	 * get list all manager
	 */
	public function manager_list(){
		$this->db->select('_id, first_name, last_name');
		$this->db->from('users');
		$this->db->where('role = 2');
		
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
	}
	
	/*
	 * add new table
	 * 
	 */
	public function create_new_table($tableInfo){
		return $this->db->insert('tables', $tableInfo);
	}
	
	/*
	 * detail of table
	 */
	public function table_detail($table_id){
		$this->db->select('t._id as _id, t.table_name as name, number, t.manager_id as manager_id, u.first_name as first_name, u.last_name as last_name, t.status as status_id, ts.status as status, t.description as description, t.image as image');
		$this->db->from('tables as t');
		$this->db->join('users as u', 'u._id = t.manager_id');
		$this->db->join('table_status as ts', 'ts._id = t.status');
		$this->db->where('t._id', $table_id);
		
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            $result = $query->result();
            return $result[0];
        }
        return false;
	}
	
	public function update_table($table_id, $tableInfo){
		$this->db->where('_id', $table_id);
		$this->db->update('tables', $tableInfo);
		$affect = $this->db->affected_rows();

		if ($affect > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public function delete_dish($table_id){
		$this->db->where('_id', $table_id);
		$this->db->delete('tables');
	}
	
	public function all_table(){
		$select_waiter_table = "select min(user_id) from bills as b where b.table_id = t._id AND b.end_time IS NULL";
		$this->db->select('t._id as _id, t.table_name as name, number, t.manager_id as manager_id, u.first_name as first_name, ('.$select_waiter_table.') as waiter_id, u.last_name as last_name, t.status as status_id, ts.status as status, extra_table, booking_status as booking_status_id, tsb.status as booking_status, booking_time, booking_description, t.description as desciption, t.image as image');
		$this->db->from('tables as t');
		$this->db->join('users as u', 'u._id = t.manager_id');
		$this->db->join('table_status as ts', 'ts._id = t.status', 'left');
		$this->db->join('table_status as tsb', 'tsb._id = t.booking_status', 'left');
        
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
	}
	
	public function get_manager_id($username){
		$this->db->select('_id');
		$this->db->where('username', $username);
		$this->db->where('role', 2);
		$this->db->from('users');
		
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            $result = $query->result();
            return $result[0]->_id;
        }
        return false;
	}
}